package cn.tedu.pet.product.controller;

import cn.tedu.pet.product.pojo.param.PetAddNewParam;
import cn.tedu.pet.product.pojo.param.PetUpdateParam;
import cn.tedu.pet.commons.pojo.vo.PageData;
import cn.tedu.pet.product.pojo.vo.PetListItemVO;
import cn.tedu.pet.product.pojo.vo.PetStandardVO;
import cn.tedu.pet.product.service.IPetService;
import cn.tedu.pet.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/pet")
@Api(tags = "01.宠物管理模块")
@Slf4j
@Validated
public class PetController {

    @Autowired
    private IPetService petService;

    @PostMapping("/add-new")
    @ApiOperation("添加宠物")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid PetAddNewParam petMessageAddNewParam){
        log.debug("开始处理[添加宠物]的请求,参数:{}",petMessageAddNewParam);
        petService.addNew(petMessageAddNewParam);
        log.debug("处理[添加宠物]的请求,完成!");
        return JsonResult.ok();
    }


    @PostMapping("/delete")
    @ApiOperation("根据ID删除宠物数据")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "宠物ID",required = true,dataType = "long")
    })
    public JsonResult delete(@Range(min = 1,message = "根据ID删除宠物失败,请提交合法的ID值!")
                             @RequestParam Long id){
        log.debug("处理[删除宠物数据]的请求,参数:{}",id);
        petService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改宠物数据")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "宠物ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@RequestParam @Range(min = 1,message = "请提交有效的相册id值!") Long id,
                                     @Valid PetUpdateParam petMessageUpdateParam) {
        log.debug("开始处理[修改宠物详情]的业务，参数：{}", petMessageUpdateParam);
        petService.updateInfoById(id,petMessageUpdateParam);
        return JsonResult.ok();
    }

    @GetMapping("/list")
    @ApiOperation("查询宠物列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult list(@Range(min = 1, message = "查询宠物列表失败，请提供正确的页码值！") Integer page) {
        log.debug("开始处理[查询宠物列表]的请求，页码：{}", page);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<PetListItemVO> pageData = petService.list(page);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/standard")
    @ApiOperation("根据ID查询宠物详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "宠物ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1, message = "根据ID查询宠物详情失败，请提交合法的ID值！")
                                      @RequestParam Long id) {
        log.debug("开始处理【根据ID查询宠物详情】的请求，参数：{}", id);
        PetStandardVO result = petService.getStandardById(id);
        return JsonResult.ok(result);
    }

}
